package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.SensitiveDto;
import com.heima.model.wemedia.pojos.WmSensitive;
import com.heima.wemedia.mapper.WmSensitiveMapper;
import com.heima.wemedia.service.WmSensitiveService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
@Transactional
@Slf4j
public class WmSensitiveServiceImpl implements WmSensitiveService {

    @Autowired
    private WmSensitiveMapper wmSensitiveMapper;


    @Override
    public ResponseResult findAll(SensitiveDto dto) {
        //1.校验参数
        if(dto == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //2.查询
        Page<WmSensitive> page = new Page<>(dto.getPage(),dto.getSize());
        LambdaQueryWrapper<WmSensitive> wrapper = new LambdaQueryWrapper<>();
        if(StringUtils.isNotBlank(dto.getName())){
            wrapper.like(WmSensitive::getSensitives,dto.getName());
        }
        wrapper.orderByDesc(WmSensitive::getCreatedTime);
        //todo 此处不需要返回值，因为查询结果会封装到参数的page对象中
        wmSensitiveMapper.selectPage(page, wrapper);
        //3.封装返回结果
        ResponseResult responseResult = new PageResponseResult(dto.getPage(),dto.getSize(), (int) page.getTotal());
        responseResult.setData(page.getRecords());
        responseResult.setCode(200);
        return responseResult;
    }

    @Override
    public ResponseResult del(Integer id) {
        if(id == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        return ResponseResult.okResult(wmSensitiveMapper.deleteById(id));
    }

    /**
     * 新增敏感词
     * @param wmSensitive
     * @return
     */
    @Override
    public ResponseResult save(WmSensitive wmSensitive) {
        //todo 1.校验参数
        if(null == wmSensitive){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //todo 2.数据不能重复添加 （需要先查询是否存在，如果存在，则不允许重复添加）
        LambdaQueryWrapper<WmSensitive> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(WmSensitive::getSensitives,wmSensitive.getSensitives());
        WmSensitive wmSensitiveDb = wmSensitiveMapper.selectOne(wrapper);
        if(wmSensitiveDb != null ){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
        }
        //补全数据
        wmSensitive.setCreatedTime(new Date());
        wmSensitiveMapper.insert(wmSensitive);
        return ResponseResult.okResult("新增成功");
    }
}
